module hong{
    /** 操作符定义 */
    const operval = ['+', '-', '×', '÷'];
    /** 显示元素 */
    export class sitem extends egret.DisplayObjectContainer {
        /** 底框 */
        private bg:egret.Bitmap;
        /** 文本显示 */
        private tx:egret.TextField;
        /** 操作值 null 特殊值当前对象是容器*/
        private val:string;
        /** 左边值 */
        private valLeft:sitem;
        /** 操作符 */
        private valOp:sitem;
        /** 右边值 */
        private valRight:sitem;
        /** 移动时对象偏远坐标x */
        public offsetX:number;
        /** 移动时对象偏远坐标y */
        public offsetY:number;
        /** 设置触摸唯一id */
        public touchId:number;

        public constructor(tv:string) {
            super();

            this.bg = new egret.Bitmap();
            this.bg.texture = RES.getRes("qp_png");
            this.addChild(this.bg);
            this.touchId = -1;

            //初始化变量
            if(tv){
                this.tx = new egret.TextField();
                this.tx.text = tv;
                this.addChild(this.tx);
                this.tx.width = this.tx.height = 90;
                this.tx.textColor = 0xFFFF00;
                this.tx.size = 60;
                this.tx.textAlign = egret.HorizontalAlign.CENTER;
                this.tx.verticalAlign = egret.VerticalAlign.MIDDLE;
                this.val = tv;
            }else this.val = null;
            this.touchEnabled = true;
            //添加事件
            //this.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onclick, this);
            //this.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.touchBegin, this);
            //this.addEventListener(egret.TouchEvent.TOUCH_END, this.touchEnd, this);
        }

        /** 添加显示对象 */
        public addVal(itl:sitem, itr:sitem){
            this.valLeft = itl;
            this.valRight = itr;
            this.valOp = new sitem('+');
        }

        /** 点击对象修改操作符 */
        public onclick(e:egret.TouchEvent):void{
            if(this.valOp){
                let opindex = operval.indexOf(this.valOp.getVal());
                if(opindex != -1){
                    opindex++;
                    if(opindex > 3) opindex = 0;
                    this.tx.text = this.val = operval[opindex];
                }
            }
        }

        /** 开始触摸对象 */
        public touchBegin(e:egret.TouchEvent):void{
            //console.log("begin", e.stageX, e.stageY, this.x, this.y);
            this.offsetX = e.stageX - this.x;
            this.offsetY = e.stageY - this.y;
            this.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.touchMove, this);
        }
        /** 移动触摸对象 */
        public touchMove(e:egret.TouchEvent):void{
            //console.log("move", e.stageX, e.stageY);
            this.x = e.stageX - this.offsetX;
            this.y = e.stageY - this.offsetY;
        }
        /** 结束触摸对象 */
        public touchEnd(e:egret.TouchEvent):void{
            this.removeEventListener(egret.TouchEvent.TOUCH_MOVE, this.touchMove, this);
            console.log("touchEnd");
        }

        /** 获取值 */
        public getVal():string{
            return this.val;
        }

        /** 获取数字值 */
        public getNum():number{
            if(!this.val){
                switch(this.valOp.getVal()){
                    case operval[0]:{
                        return this.valLeft.getNum() + this.valRight.getNum()
                    }case operval[1]:{
                        return this.valLeft.getNum() - this.valRight.getNum()
                    }case operval[2]:{
                        return this.valLeft.getNum() * this.valRight.getNum()
                    }case operval[3]:{
                        return this.valLeft.getNum() / this.valRight.getNum()
                    }
                }
            }else{
                return Number(this.val);
            }
        }
    }
}